const CompressionWebpackPlugin = require("compression-webpack-plugin");

const path = require("path");
module.exports = {
  baseUrl: process.env.NODE_ENV === "production" ? "/Public/admin/" : "./",
  // 生产环境是否生成 sourceMap 文件
  productionSourceMap: true,
  configureWebpack: () => {
    if (process.env.NODE_ENV === "production") {
      return {
        plugins: [
          new CompressionWebpackPlugin({
            test: /\.(js|css|woff|ttf|png|jpg)$/,
            threshold: 10240
          })
        ]
      };
    }
  },
  chainWebpack: config => {
    const types = ["vue-modules", "vue", "normal-modules", "normal"];
    types.forEach(type =>
      addLessResource(config.module.rule("less").oneOf(type))
    );
  },
  css: {
    loaderOptions: {
      // 向 CSS 相关的 loader 传递选项
      less: {
        javascriptEnabled: true
      }
    }
  },
  // webpack-dev-server 相关配置
  devServer: {
    https: false,
    port: "9999",
    // 设置代理
    proxy: {
      "/apis": {
        target: "http://www.researchera.cn/admin.php",
        changeOrigin: true,
        pathRewrite: {
          "^/apis": ""
        }
      }
    }
  }
};

function addLessResource(rule) {
  rule
    .use("style-resource")
    .loader("style-resources-loader")
    .options({
      patterns: [path.resolve(__dirname, "./src/assets/css/mixins.less")]
    });
}
